<?php
/**
 * Created by PhpStorm.
 * User: Smlie
 * Date: 2016/3/16
 * Time: 19:44
 */
namespace Admin\Controller;
use Think\Controller;
/*
 * 全局控制器
 * */
class PaymentController extends BaseController {

    //设置/取消VIP
    public function set(){

        $user_id = I('user_id');//用户ID


        $_where['user_id'] = $user_id;
        $_where_order_field['where'] = $_where;
        $_where_order_field['field'] = 'add_time,admin_id,status,remark';
        $_where_order_field['order'] = 'add_time desc';

        $_datalist = $this->_getListsFromModel('vip_changes', $_where_order_field);
        foreach($_datalist['lists'] as $key=>$value ){
            $value["admin_user_name"] =M('admin_user') ->where(array("user_id" => $value["admin_id"]))->getField('user_name');
            $_datalist["lists"][$key] = $value;
        } 
        $this->assign('_datalist', $_datalist);
        $this->display();
    }


    public function set_vip(){

        if(IS_POST){
            $user_name= I('user_name');//登录账号
            $is_vip = I('is_vip');//是否为VIP
            $data['is_vip'] =$is_vip;

            M("users")->where(array("user_name"=>$user_name))->save($data);

            $data1['user_id'] = I('user_id');//用户ID
            $data1['add_time'] = time();//添加时间
            $data1['user_name'] = $user_name;//登录账号
            $data1['mobile'] = I('mobile');//手机号码
            $data1['admin_id'] = UID;//后台管理员ID
            $data1['is_apply_for_cancel'] = 0;//是否有申请取消
            $data1['apply_for_cancel_time'] = 0;//申请取消的时间
            $data1['user_rank'] = $is_vip;//会员等级
            $data1['status'] = $is_vip;//状态
            $data1['remark'] = I('remark');//备注

            M("vip_changes")->add($data1);

            $exe_scriptstr = "<script>
                setTimeout(function(){
                           window.location.reload();
                },2000);

           </script>";


            //添加成功后，输出文本，把js脚本一起输出，前台只要html到某个div里就能够执行js
            $this->success('操作成功！' . $exe_scriptstr);

        }

        $user_id=I('user_id');

        $this->display();
    }

    public function search_user(){
        $user_id= I("request.user_id");
        $entity = M('users')->field('nickname,is_vip,user_money,mobile_phone,user_name')->where(array('user_id'=>$user_id))->find();
        if(!$entity){

            $arr['status']=0;
            $arr['info']='没有查询到该账户';
            echo json_encode($arr);
            exit;

        }
        $arr['a']=$entity['nickname'];
        $arr['b']=$entity['mobile_phone'];
        $arr['c']=$entity['user_money'];
        $arr['d']=$entity['is_vip'];
        $arr['e']=$entity['user_name'];
        $arr['status']=1;
        echo json_encode($arr);
        exit;

    }


//    /* 订单状态 */
//define('OS_UNCONFIRMED',            0); // 未确认
//define('OS_CONFIRMED',              1); // 已确认
//define('OS_CANCELED',               2); // 已取消
//define('OS_INVALID',                3); // 无效
//define('OS_RETURNED',               4); // 退货
//define('OS_SPLITED',                5); // 已分单
//define('OS_SPLITING_PART',          6); // 部分分单




    public function search_order_info(){
        $order_number= I("request.order_number",0,'trim' );

        $entity = M('order_info')->field('user_id,order_status,order_amount,money_paid')->where(array('order_sn'=>$order_number))->find();

        if(!$entity){

            $arr['status']=0;
            $arr['info']='没有查询到该订单';
            echo json_encode($arr);
            exit;

        }





        $arr['a']=$entity['user_id'];

        if($entity['order_status'] ==1){
            $entity['order_status'] = '确认';
        }elseif($entity['order_status'] ==2){
            $entity['order_status'] = '已取消';
        }elseif($entity['order_status'] ==3){
            $entity['order_status'] = '无效';
        }elseif($entity['order_status'] ==4){
            $entity['order_status'] = '退货';
        }elseif($entity['order_status'] ==5){

        }elseif($entity['order_status'] ==6){

        }elseif($entity['order_status'] ==0){
            $entity['order_status'] = '未确认';
        }






        $entity2 = M('users')->field('nickname,is_vip,user_money,mobile_phone,user_id')->where(array('user_id'=>$entity['user_id']))->find();

        $arr['a1']=$entity2['nickname'];
        $arr['b1']=$entity2['mobile_phone'];
        $arr['c1']=$entity2['user_money'];
        $arr['d1']=$entity2['is_vip'];



        $arr['b']=$entity['order_status'];
        $arr['c']=$entity['money_paid'];



        $arr['status']=1;
        echo json_encode($arr);
        exit;

    }







    //赠款规则配置
    public function grant(){
        $this->display();
    }

    public function grant_list(){

        $_listrows = I('request._listrows', '', 'trim');
        $_where_order_field['id']='load_tb_div';
        $_where_order_field['listrows'] = $_listrows;


        $_datalist = $this->_getListsFromModel('present_money_set', $_where_order_field);
        $this->assign('_datalist', $_datalist);

        $this->display();
    }

    public function add_grant(){
        $is_add=I("request.is_add",0);
        if(intval($is_add)==0){
            $this->grant_add();
        }else{
            $this->grant_update();
        }
    }

    /**
     * 添加方法
     */
    public  function grant_add(){

        if(IS_POST){

            $data['money_start'] = I('money_start');//预存货款开始值
            $data['money_end'] = I('money_end');//预存货款结束值
            $data['every_money'] = I('every_money');//每多少元
            $data['send_money'] = I('send_money');//赠送金额
            $data['status'] = I('status');//状态

            M("present_money_set")->data($data)->add();
            $exe_scriptstr = "<script>
                setTimeout(function(){
                var win=   art.dialog.opener;
                                win.load();
                                art.dialog.close();
                },2000);

           </script>";


            //添加成功后，输出文本，把js脚本一起输出，前台只要html到某个div里就能够执行js
            $this->success('添加成功！' . $exe_scriptstr);

        }else{
            $this->display();
        }
    }

    /**
     * 更新方法
     */
    public  function  grant_update(){

        $id=I("request.id");
        if(IS_POST){
            $data['money_start'] = I('money_start');//预存货款开始值
            $data['money_end'] = I('money_end');//预存货款结束值
            $data['every_money'] = I('every_money');//每多少元
            $data['send_money'] = I('send_money');//赠送金额
            $data['status'] = I('status');//状态

            M("present_money_set")->where(array("id"=>$id))->save($data);

            $exe_scriptstr = "<script>
                setTimeout(function(){
                var win=   art.dialog.opener;
                                win.load();
                                art.dialog.close();
                },2000);

           </script>";

            //添加成功后，输出文本，把js脚本一起输出，前台只要html到某个div里就能够执行js
            $this->success('修改成功！' . $exe_scriptstr);

        }else{

            $entity = M('present_money_set')->where(array('id'=>$id))->find();

            $this->assign('entity',$entity);
            $this->assign("is_add",1);
            $this->display();
        }
    }


    public function delete(){

        $id = I('request.id',0,'intval');
        if(empty($id)){
            $this->error('参数错误');
            exit;
        }
        M('present_money_set')->where(array('id'=>$id))->delete();//删除赠款规则
        $this->success('删除成功！');



    }

    public function red(){


        $this->display();
    }

    public function add_red(){

        $this->display();

    }

    public function red_money(){

        if(IS_POST){
            $user_name= I('username');//登录账号
            $in_or_out = I('in_or_out');//出入账
            $money = I('money');//变动金额
            $user_Entity =  M("users")->where(array("user_name"=>$user_name))->Field('user_money,user_id')->find();
            if(empty( $user_Entity)){
                $this->error('用户不存在，请检查用户名是否正确！');
            }
            $user_money=$user_Entity['user_money']; //用户余额
            if($in_or_out =='IN'){
                M("users")->where(array("user_name"=>$user_name))->setInc('user_money',$money);
                $user_money=$user_money+$money;
            }else{

                if($money>$user_money){
                    $this->error('您扣除的红冲金额大于用户总金额，请重新输入！');
                }
                M("users")->where(array("user_name"=>$user_name))->setDec('user_money',$money);
                $money*=-1;
                $user_money=$user_money+$money;
            }

            $data1['user_id'] = I('user_id');//用户ID
            $data1['user_name'] = $user_name;//登录账号
            $data1['mobile'] = I('mobile');//手机号码
            $data1['add_time'] = time();//添加时间
            $data1['in_or_out'] = $in_or_out;//出入账
            $data1['money'] = $money;
            $data1['gathering_way'] =2;
            $data1['transaction_type'] = 4;//交易类型
            $data1['source_type'] = 4;//流水记录来源类别
            $data1['reference'] = I('reference');//交易参考
            $data1['remark'] = I('remark');//备注
            $data1['gathering_time'] = date("Y-m-d H:i:s",time());//最后操作时间
            $data1['user_balance_details'] = $user_money;//用户当前余额

            M("purchase_price_list")->add($data1);

            $exe_scriptstr = "<script>
                setTimeout(function(){
					 var win = art.dialog.open.origin; //获取来源窗口 用 win 接收
                           win.reload_data();
                           art.dialog.close();
                },1000);
           </script>";


            //添加成功后，输出文本，把js脚本一起输出，前台只要html到某个div里就能够执行js
            $this->success('操作成功！' . $exe_scriptstr);

        }
        $this->display();
    }

    //红冲充值 异步列表数据 20160408 ================================
    public  function  ajax_red_list(){

        $_listrows = I('request._listrows', '', 'trim');

        $_where['transaction_type'] = 4;

        $gathering_way = I('request.gathering_way',0,'intval');
        if(!empty($gathering_way)){
            $_where['gathering_way']=$gathering_way;
        }
        $keywords = I('request.keywords','','trim');
        if(!empty($keywords)){
            $_where['user_name|mobile|gathering_man|bank_transaction_id']=array('like',"%{$keywords}%");
        }

        $where_order['where'] = $_where;
        $where_order['order'] = ' add_time desc ';
        $where_order['listrows'] = $_listrows;   //分页条件
        $where_order["id"]="load_tb_div";
        $where_order["field"]="user_name,mobile,money,reference,remark";
        // 		$where_order["field"]="js_suppliers.logo_img,js_suppliers.code,js_country.country_id,js_country.country_title,js_suppliers.suppliers_id,js_suppliers.suppliers_name,js_suppliers.remark,js_suppliers.address,js_suppliers.mobile,js_suppliers.linkman,js_suppliers.is_dismantle,js_suppliers.aliasname";
        // 		$where_order["join"]="inner join js_country  on js_suppliers.country_id=js_country.country_id";
        $_datalist = $this->_getListsFromModel('purchase_price_list', $where_order);
        $this->assign('_datalist', $_datalist);
        // print_r($_datalist);
        //exit;
        $this->display();

    }


    //赠款规则配置
    public function vip_index(){
        $this->display();
    }

    public function vip_list(){

        $_listrows = I('request._listrows', '', 'trim');
        $_where_order_field['id']='load_tb_div';
        $_where_order_field['listrows'] = $_listrows;
        $_where_order_field['order']='add_time desc';

        $user_name = I('request.user_name','','trim');

        if (!empty($user_name)) {
            $_where['user_name']=array('like',"%$user_name%");
        }

        $_where_order_field['where'] = $_where;

        $_datalist = $this->_getListsFromModel('vip_changes', $_where_order_field);

        foreach($_datalist['lists'] as $key=>$value ){
            $value["nickname"] =M('users') ->where(array("user_id" => $value["user_id"]))->getField('nickname');
            $value["admin_user_name"] =M('admin_user') ->where(array("user_id" => $value["admin_id"]))->getField('user_name');
            $_datalist["lists"][$key] = $value;
        }
        $this->assign('_datalist', $_datalist);

        $this->display();
    }


    public function update(){

        $vip_changes_list = M('vip_changes')->where(array('user_id'=>0))->getField('user_name',true);




        foreach( $vip_changes_list as $key=>$value){

            $user_id=M('users')->where(array('user_name'=>$value))->getField('user_id');

            $data['user_id']=$user_id;
            M('vip_changes')->where(array('user_name'=>$value))->save($data);
        }


        echo '操作成功';
        exit;
    }

}
